package Hangman;

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The word guessed in Last Man Standing
 * @author Rebecca
 */
public class Word {
    class Letter {
        char character;
        boolean isGuessed = false;
    }

    Letter[] letters;
    private int minLength = 4;
    private int maxLength = 8;

    public Word(String playerWord) {
        if (playerWord == null) playerWord = "";
        letters = new Letter[playerWord.length()];
        if (playerWord != null)
        {
            for (int i=0; i<playerWord.length(); i++)
            {
                Letter letter = new Letter();
                letter.character = playerWord.charAt(i);
                letters[i] = letter;
            }
        }
    }

    public String toString() {
        String w = "";
        for (int i=0; i<letters.length; i++) {
            //System.out.println(letters.get(i) + "%");
            w += letters[i].character;
        }
        //System.out.println(word);
        return w;
    }

    //Checks whether the word has any non-alphabetical characters
    public boolean alphabetical() {
        char[] alphabet = new char[]{'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd',
        'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k',
        'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r',
        'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y',
        'Z', 'z'};

        for (Letter e : letters) {
            boolean alphabetical = false;
            for (char c : alphabet) if (e.character == c) alphabetical = true;
            if (alphabetical == false) return false;
        }
        return true;
    }

    public boolean initializeWord() {
        if (!alphabetical()) return false;
        String word = toString();
        if (word.length() < minLength || word.length() > maxLength) return false;

        File file = new File("/Users/stuartsealfon/NetBeansProjects/LMSProject/LMSProject-ejb/src/java/words");
        try {
            Scanner scan = new Scanner(file);
            while (scan.hasNextLine()) {
                String nextDictWord = scan.nextLine();
                //System.out.println(nextDictWord);
                if (word.equalsIgnoreCase(nextDictWord)) return true;
            }
        } catch (FileNotFoundException ex) {
            System.out.println(ex);
            return false;
        }
        return false;
    }

    public void guessWord(char c) {
        for (int i=0; i<letters.length; i++) {
            if (c == letters[i].character) {
                letters[i].isGuessed = true;
            }
        }
    }

    public int getMaxLength() {
        return maxLength;
    }

    public char getLetter(int i) {
        char letter = letters[i].character;
        if (letters[i].isGuessed) return letter;
        else return '_';
    }
}
